<?php


namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * h会议安排
 */
class Meeting extends Api
{

    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
      *  会议信息
     */
    public function gettodaymeeting()
    {

        $user=$this->auth->getUser();

        $team_id=$user['team_id'];

        $meeting=Db::name('meeting')
            ->where(function ($query) use ($team_id) {
                $teamIds = is_array($team_id) ? $team_id : explode(',', $team_id);
                foreach ($teamIds as $id) {
                    $query->whereOr('team_id', 'like', '%'.$id.'%');
                }
            })
            ->where('datetime','>',time())
//            ->where('datetime','lt',strtotime(date('Y-m-d'))+86400)
            ->order('id asc')
            ->find();

        if ($meeting){
            $meeting['datetime']=date('Y-m-d H:i',$meeting['datetime']);
        }

        $this->success('会议信息',$meeting);

    }


    /**
      *  日期
     **/

    public function getmtdate(){

//        获取九月的每一天

        $month=date('m');
        //本月有多少天


        $day=date('t',$month);




        $array=[];

        for ($i=1;$i<=$day;$i++){



            if($i<10){
                $date=date('Y-m').'-0'.$i;
            }else{
                $date=date('Y-m').'-'.$i;
            }

            $arr['date']=$date;

            $arr['day']=$i;

            $array[]=$arr;

        }

        $this->success('会议信息',['all'=>$array,'today'=>date('Y-m-d')]);

    }

    /**
     * 获取会议信息
     */
    public function getmeetbyday(){

        $day=$this->request->request('day');

        $user=$this->auth->getUser();

        $team_id=$user['team_id'];

        $meeting=Db::name('meeting')
            ->where(function ($query) use ($team_id) {
                $teamIds = is_array($team_id) ? $team_id : explode(',', $team_id);
                foreach ($teamIds as $id) {
                    $query->whereOr('team_id', 'like', '%'.$id.'%');
                }
            })
            ->where('datetime','>',strtotime($day))
            ->where('datetime','lt',strtotime($day)+86400)
            ->order('id asc')
            ->select();

        if ($meeting){
            foreach ($meeting as $key=>$value){
                $meeting[$key]['datetime']=date('m-d H:i',$value['datetime']);

                $qiandao=Db::name('meeting_sign')
                    ->where('meeting_id',$value['id'])
                    ->where('user_id',$this->auth->id)
                    ->find();

                if ($qiandao){
                    $meeting[$key]['qiandao']=1;
                }else{
                    $meeting[$key]['qiandao']=0;
                }


            }
        }


        $this->success('会议信息',$meeting);



    }

    //签到

    public function qiandao(){

        $id=$this->request->request('id');
        $user=$this->auth->getUser();

        $qiandao=Db::name('meeting_sign')
            ->where('meeting_id',$id)
            ->where('user_id',$this->auth->id)
            ->find();

        if ($qiandao){
            $this->error('已签到');
        }


        $meeting=Db::name('meeting')
            ->where('id',$id)
            ->find();


        //会议前30分和开始后30分之间可签到
        if ($meeting['datetime']<(time()-1800) || $meeting['datetime']>(time()+1800) ){
            $this->error('当前时间无法签到');
        }




        Db::name('meeting_sign')
            ->data(['meeting_id'=>$id,'user_id'=>$user['id'],'addtime'=>time()])
            ->insert();

        $this->success('签到成功');

    }


    /**
     * 单个会议详情
     */

    public function getmeetbyid(){

        $id=$this->request->request('id');

        $user=$this->auth->getUser();

        $uid=$user['id'];

        $meeting=Db::name('meeting')
            ->where(['id'=>$id])
            ->find();


        $qiandao=Db::name('meeting_sign')
            ->where('meeting_id',$id)
            ->where('user_id',$this->auth->id)
            ->find();

        if ($qiandao){
            $meeting['qiandao']=1;
        }else{
            $meeting['qiandao']=0;
        }


        $this->success('会议信息',$meeting);



    }




}